package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.CourseOrder;
import cn.wolfcode.crm.domain.Customer;
import cn.wolfcode.crm.domain.SystemDictionaryItem;
import cn.wolfcode.crm.query.CourseorderQuery;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.ICourseOrderService;
import cn.wolfcode.crm.service.ICustomerService;
import cn.wolfcode.crm.service.ISystemDictionaryItemService;
import cn.wolfcode.crm.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/courseOrder")
public class CourseOrderController {
    @Autowired
    private ICourseOrderService courseOrderService;
    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;
    @Autowired
    private ICustomerService customerService;

    //提供一个方法处理查询所有部门的请求,响应HTML
    @RequestMapping("/list")
   // @RequiredPermission(name = "部门列表")
    @RequiresPermissions(value = {"courseOrder:list","部门列表"}, logical=Logical.OR)
    public String list(Model model, @ModelAttribute("qo")CourseorderQuery qo) {
        //这里不再是调用listAll的方法,而是我嗯创建的query方法
        PageInfo pageInfo = courseOrderService.query(qo);
        model.addAttribute("result", pageInfo);
        model.addAttribute("subject", systemDictionaryItemService.selectByDicSn("subject"));
        //课程
        List<SystemDictionaryItem> courses = systemDictionaryItemService.selectByCourse();
        model.addAttribute("courses",courses);

        //查询所有的
        List<Customer> customers = customerService.listAll();
        model.addAttribute("customers",customers);
        return "courseOrder/list";
    }


    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions(value = {"saveOrUpdate:list","部门保存/更新"}, logical=Logical.OR)//or只要有一个就可以运行
    @ResponseBody
    public JsonResult saveOrUpdate(CourseOrder courseOrder) {

            if (courseOrder.getId() == null) {
                courseOrderService.save(courseOrder);
            } else {
                courseOrderService.update(courseOrder);
            }
            return new JsonResult();

    }

}




